package com.lcq.demo.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.lcq.demo.common.entity.PageResult;
import com.lcq.demo.common.entity.Result;
import com.lcq.demo.entity.dto.NewsDTO;
import com.lcq.demo.service.NewsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

        import javax.annotation.Resource;

/**
 * @author 240701005
 */
@Tag(name = "资讯管理")
@RestController
@RequestMapping("/api/news")
@Slf4j
public class NewsController {

    @Resource
    private NewsService newsService;

    @Operation(summary = "根据Id查询新闻")
    @GetMapping("/{id}")
    public Result<NewsDTO> getById(@PathVariable Long id) {
        try {
            if (log.isInfoEnabled()) {
                log.info("NewsController.getById.id:{}", id);
            }
            Preconditions.checkNotNull(id, "id不能为空");
            return Result.ok(newsService.getById(id));
        } catch (Exception e) {
            log.error("NewsController.getById.error:{}", e.getMessage(), e);
            return Result.fail("查询新闻失败！");
        }
    }

    @Operation(summary = "新增新闻")
    @PostMapping
    public Result<Boolean> addNews(@RequestBody NewsDTO newsDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("NewsController.addNews.newsDTO:{}", newsDTO);
            }
            Preconditions.checkArgument(!StringUtils.isEmpty(newsDTO.getNewsTitle()), "新闻标题不能为空！");
            Preconditions.checkArgument(!StringUtils.isEmpty(newsDTO.getNewsContent()), "新闻内容不能为空！");
            Preconditions.checkNotNull(newsDTO.getNewsTime(), "新闻发布时间不能为空！");
            return Result.ok(newsService.addNews(newsDTO));
        } catch (Exception e) {
            log.error("NewsController.addNews.error:{}", e.getMessage(), e);
            return Result.fail("添加新闻失败！");
        }
    }

    @Operation(summary = "编辑新闻")
    @PutMapping
    public Result<Boolean> updateNews(@RequestBody NewsDTO newsDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("NewsController.updateNews.newsDTO:{}", newsDTO);
            }
            Preconditions.checkNotNull(newsDTO.getId(), "新闻Id不能为空！");
            Preconditions.checkArgument(!StringUtils.isEmpty(newsDTO.getNewsTitle()), "新闻标题不能为空！");
            Preconditions.checkArgument(!StringUtils.isEmpty(newsDTO.getNewsContent()), "新闻内容不能为空！");
            Preconditions.checkNotNull(newsDTO.getNewsTime(), "新闻发布时间不能为空！");
            return Result.ok(newsService.updateNews(newsDTO));
        } catch (Exception e){
            log.error("NewsController.updateNews.error:{}", e.getMessage(), e);
            return Result.fail("编辑失败！");
        }
    }

    @Operation(summary = "上/下架新闻")
    @PostMapping("/listOrDelist")
    public Result<Boolean> listOrDelistNews(@RequestBody NewsDTO newsDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("NewsController.listOrDelistNews.newsDTO:{}", newsDTO);
            }
            Preconditions.checkNotNull(newsDTO.getId(), "新闻Id不能为空！");
            return Result.ok(newsService.listOrDelistNews(newsDTO));
        } catch (Exception e){
            log.error("NewsController.listOrDelist.error:{}", e.getMessage(), e);
            return Result.fail("上/下架失败！");
        }
    }

    @Operation(summary = "删除新闻")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteNews(@PathVariable Long id) {
        try {
            if (log.isInfoEnabled()) {
                log.info("NewsController.deleteNews.id:{}", id);
            }
            Preconditions.checkNotNull(id, "id不能为空");
            return Result.ok(newsService.deleteNews(id));
        } catch (Exception e) {
            log.error( "NewsController.deleteNews.error:{}", e.getMessage(), e);
            return Result.fail("删除新闻失败！");
        }
    }

    @Operation(summary = "获取新闻列表")
    @PostMapping("/list")
    public Result<PageResult<NewsDTO>> getNewsList(@RequestBody NewsDTO newsDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("NewsController.getNewsList.NewsDTO:{}", JSON.toJSONString(newsDTO));
            }
            return Result.ok(newsService.getNewsList(newsDTO));
        } catch (Exception e) {
            log.error("NewsController.getNewsList.error:{}", e.getMessage(), e);
            return Result.fail("获取新闻列表失败！");
        }
    }
}
